#include "stdafx.h"

#include "EngineTests.h"

using namespace System;
using namespace CzechMoveGen;

//bool BitBoardMatchTest2()
//{
//	int bbLogId = LogInit("c:\\matchresult2.txt");
//
//	BitBoard bitBoard;
//	SetStartUpPositionBitBoard(&bitBoard);
//
//	bool oError = true;
//
//	try
//	{
//		bool bWhite = true;
//		for(int i = 0; i < 100; i++)
//		{
//			Console::WriteLine(L"Move: {0}", i);
//
//			// compute move
//			BitBoardMove bbMove;		
//			int result = NegamaxBB(6, &bitBoard, bWhite, &bbMove);
//
//			// log move
//			LOG_MSG(bbLogId, "Move: %d\n", i);
//			LogBitBoard(bbLogId, bitBoard);
//			LogBitBoard(bbLogId, bbMove);
//
//			// do move
//			ToggleMove(&bitBoard, &bbMove);
//
//			if(result == GAME_WIN || result == GAME_LOSS)
//				break;
//
//			bWhite = ! bWhite;
//		}
//
//
//		oError = false;
//	}
//	catch(...)
//	{
//	}
//
//	LogClose(bbLogId);
//
//	return oError;
//}


//bool BitBoardMatchTest3()
//{
//	int bbLogId = LogInit("c:\\matchresult3.txt");
//
//	BitBoard bitBoard;
//	SetStartUpPositionBitBoard(&bitBoard);
//
//	bool oError = true;
//
//	try
//	{
//		bool bWhite = true;
//		for(int i = 0; i < 100; i++)
//		{
//			Console::WriteLine(L"Move: {0}", i);
//
//			// compute move
//			BitBoardMove bbMove;		
//			int result = 0;
//			if(bWhite)
//				result = NegamaxBB(6, &bitBoard, bWhite, &bbMove);
//			else
//			{
//				result = AlphaBetaBB(&bitBoard, bWhite, 5000, &bbMove);
//				Console::WriteLine("Depth: {0}", iReachedDepth);
//			}
//
//			// log move
//			LOG_MSG(bbLogId, "Move: %d\n", i);
//			LogBitBoard(bbLogId, bitBoard);
//			LogBitBoard(bbLogId, bbMove);
//
//			// do move
//			ToggleMove(&bitBoard, &bbMove);
//
//			if(result == GAME_WIN || result == GAME_LOSS)
//				break;
//
//			bWhite = ! bWhite;
//		}
//
//
//		oError = false;
//	}
//	catch(...)
//	{
//	}
//
//	LogClose(bbLogId);
//
//	return oError;
//}


bool BitBoardMatchTest4()
{
	int bbLogId = LogInit("c:\\matchresult4.txt");

	BitBoard bitBoard;
	SetStartUpPositionBitBoard(&bitBoard);

	bool oError = true;
	bUseEndGameDB = false;


	try
	{
		bool bWhite = true;
		for(int i = 0; i < 100; i++)
		{
			Console::WriteLine(L"Move: {0}", i);

			// compute move
			BitBoardMove bbMove;		
			int result = 0;
			if(bWhite)
				bUseHistoryTables = true;
			else
				bUseHistoryTables = false;


			memset(&bbMove, 0, sizeof(BitBoardMove));
			result = AlphaBetaBB(&bitBoard, bWhite, 3000, &bbMove);
			Console::WriteLine("Depth: {0}", iReachedDepth);


			// log move
			LOG_MSG(bbLogId, "Move: %d\n", i);
			LogBitBoard(bbLogId, bitBoard);
			LogBitBoard(bbLogId, bbMove);

			// check end
			if(bbMove.wm == 0 && bbMove.wk == 0 && bbMove.bm == 0 && bbMove.bk == 0)
				break;


			// do move
			ToggleMove(&bitBoard, &bbMove);

			bWhite = ! bWhite;
		}


		oError = false;
	}
	catch(...)
	{
	}

	LogClose(bbLogId);

	return oError;
}



bool BitBoardMatchTest5()
{
	int bbLogId = LogInit("c:\\Log\\matchresult5.txt");

	BitBoard bitBoard;
	SetStartUpPositionBitBoard(&bitBoard);

	bool oError = true;
	bUseEndGameDB = false;


	try
	{
		bool bWhite = true;
		for(int i = 0; i < 100; i++)
		{
			Console::WriteLine(L"Move: {0}", i);

			// compute move
			BitBoardMove bbMove;		
			int result = 0;

			memset(&bbMove, 0, sizeof(BitBoardMove));
			if(bWhite)
			{
				bUsePositionEvaluation = false;
				//result = PVSearchBB(&bitBoard, bWhite, 5000, &bbMove);
			}
			else
			{
				bUsePositionEvaluation = false;
				result = AlphaBetaBB(&bitBoard, bWhite, 5000, &bbMove);
			}
			Console::WriteLine("Depth: {0}", iReachedDepth);


			// log move
			LOG_MSG(bbLogId, "Move: %d\n", i);
			LogBitBoard(bbLogId, bitBoard);
			LogBitBoard(bbLogId, bbMove);

			// check end
			if(bbMove.wm == 0 && bbMove.wk == 0 && bbMove.bm == 0 && bbMove.bk == 0)
				break;


			// do move
			ToggleMove(&bitBoard, &bbMove);

			bWhite = ! bWhite;
		}


		oError = false;
	}
	catch(...)
	{
	}

	LogClose(bbLogId);

	return oError;
}


bool BitBoardMatchTest6()
{
	int bbLogId = LogInit("c:\\Log\\matchresult6.txt");

	BitBoard bitBoard;
	SetStartUpPositionBitBoard(&bitBoard);

	bool oError = true;
	bUseEndGameDB = false;


	try
	{
		bool bWhite = true;
		for(int i = 0; i < 100; i++)
		{
			Console::WriteLine(L"Move: {0}", i);

			// compute move
			BitBoardMove bbMove;		
			int result = 0;

			memset(&bbMove, 0, sizeof(BitBoardMove));
			bUsePositionEvaluation = bWhite != true;	
			result = AlphaBetaBB(&bitBoard, bWhite, 5000, &bbMove);
			Console::WriteLine("Depth: {0}", iReachedDepth);


			// log move
			LOG_MSG(bbLogId, "Move: %d\n", i);
			LogBitBoard(bbLogId, bitBoard);
			LogBitBoard(bbLogId, bbMove);

			// check end
			if(bbMove.wm == 0 && bbMove.wk == 0 && bbMove.bm == 0 && bbMove.bk == 0)
				break;


			// do move
			ToggleMove(&bitBoard, &bbMove);

			bWhite = ! bWhite;
		}


		oError = false;
	}
	catch(...)
	{
	}

	LogClose(bbLogId);

	return oError;
}

